package com.titan.demo.camel.developers.SpringRoute.run;

import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;

import com.titan.demo.camel.developers.SpringRoute.domain.BodyDomain;
import com.titan.demo.camel.developers.SpringRoute.services.Utils;
import com.titan.demo.camel.utils.CamelUtils;


/**
 * 使用camel方式运行
 *
 * @author Administrator
 *
 */
public class CamelRun {
    public static void main(String[] args) {
        try {
            String[] paths = new String[]{   
                    "classpath:zzz/developers/SpringRoute/config/Services-Spring.xml"
                   ,"classpath:zzz/developers/SpringRoute/config/Camel-Spring.xml"
            };
            Utils.init(paths);
            
            BodyDomain body = new BodyDomain();
            body.append("start:");
            body.put("c01", true);
            body.put("c02", true);
            body.put("c03", true);
            
            long time = System.currentTimeMillis();
            execute(body);
            time = System.currentTimeMillis() - time;
            System.out.println("执行用时："+time+"毫秒");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private static void execute(BodyDomain body) {
        CamelContext context = null;
        ProducerTemplate template = null;
        try {
            context = Utils.getCamelContext("camel-run");
            
            template = context.createProducerTemplate();
            template.start();
            context.start();
            template.sendBody("direct:start", body);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            CamelUtils.close(template);
            CamelUtils.close(context);
        }        
    }
}

